﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Manage
{
    public partial class UserManage : PageBaseBackend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //bindRole();
                bindUser();
            }
        }

        //绑定用户
        private void bindUser()
        {
            Model.NetPager pager = new Model.NetPager();
            pager.TableName = "UserInfo";
            pager.PrimaryKey = "UserID";
            pager.Order = "CreateDate desc,UserID desc";
            pager.SortType = (int)Common.Enum.SortType.Multi;
            pager.Where = "";
            pager.PageIndex = AspNetPager1.CurrentPageIndex;
            pager.PageSize = AspNetPager1.PageSize;
            repUser.DataSource = BLL.UserInfoBLL.Instance.GetPagedList(pager);
            repUser.DataBind();
            AspNetPager1.RecordCount = pager.TotalCount;
        }

       

        //通过审核
        protected void lbtnState_Click(object sender, EventArgs e)
        {
            LinkButton lbtn=(LinkButton)sender;
            string userid = lbtn.CommandArgument;
            Model.UserInfo user = BLL.UserInfoBLL.Instance.GetDetail(userid);
            //通过审核
            if (user.State == (int)Common.Enum.auvState.Disable)
            {
                user.State = (int)Common.Enum.auvState.Enabled;
            }
            BLL.UserInfoBLL.Instance.Update(user);
            bindUser();
        }
        //设为管理员
        protected void btnSetManager_Click(object sender, EventArgs e)
        {
            Model.UserInfo user = BLL.UserInfoBLL.Instance.GetDetailByName(txtManager.Text.Trim());
            if (user != null)
            {
                txtManager.Text = "";
                user.RoleID = BLL.RoleInfoBLL.Instance.GetRoleIDByName(MANAGER);
                BLL.UserInfoBLL.Instance.Update(user); 
                bindUser();
                lbMessage.Text = "操作成功！";
            }
            else
            {
                lbMessage.Text = "找不到该用户！";
            }
        }

        //启用/禁用
        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            LinkButton lbtn = (LinkButton)sender;
            string userid = lbtn.CommandArgument;
            string command = lbtn.CommandName;
            Model.UserInfo user = BLL.UserInfoBLL.Instance.GetDetail(userid);
            if (command == "Disable")
            {
                //查看用户是否有可用服务
                IList<Model.ServiceInfo> list = BLL.ServiceInfoBLL.Instance.GetListByStatus(userid, Common.Enum.ServiceStatus.Active, false);
                if (list.Count > 0)
                {
                    AlertGo("请先删除该用户的可用订单！", "ServiceManage.aspx?username="+user.UserName+"&isdel=false ");
                }
                else
                {
                    //禁用用户
                    user.IsDel = (int)Common.Enum.auvDel.Del;
                    BLL.UserInfoBLL.Instance.Update(user);
                }
            }
            else if(command=="Enable")
            {
                //启用用户
                user.IsDel = (int)Common.Enum.auvDel.NotDel;
                BLL.UserInfoBLL.Instance.Update(user);
            }
            
            bindUser();
        }

        //换页
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            bindUser();
        }

        
    }
}